package com.zjk.controller.member;

import com.github.pagehelper.PageInfo;
import com.zjk.entity.Result;
import com.zjk.pojo.Member;
import com.zjk.pojo.SysUser;
import com.zjk.service.IMemberService;
import com.zjk.service.impl.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private IMemberService memberService;
    //用户列表
    @RequestMapping("/memberList")
    public  String memberList(String name,Model model,
                            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize",defaultValue = "2")   Integer pageSize
    ){

        List<Member> allMembers = memberService.getAllMembers(name,pageNum,pageSize);
        PageInfo<Object> pageInfo = new PageInfo<>(allMembers);
        model.addAttribute("memberList",allMembers);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("name",name);
        return "member_list";
    }

    @RequestMapping("/main")
    public String main(HttpSession httpSession,Model model){
        Member memberSession = (Member) httpSession.getAttribute("MEMBER_SESSION");
        model.addAttribute(memberSession);
        return "main";
    }
    //新增用户列表
    @RequestMapping("/addMember")
    @ResponseBody
    public Result addMember(Member member){
        Result<Object> result = new Result<>();

        if (member!=null && member.getName()!=null
                && !member.getName().trim().equals("")
                && member.getEmail()!=null
                && !member.getEmail().trim().equals("")){

            Member member1 = memberService.getMemberByMemberName(member.getName());
            System.out.println(member1);
            if (member1!=null){
                result.setMsg("用户已经存在");
                result.setSuccess(false);
                return result;
            }
            int i = memberService.addMember(member);
            System.out.println(i);
            if (i>0){
                result.setMsg("新增成功");
                result.setSuccess(true);
                return result;
            }else {
                result.setMsg("新增失败");
                result.setSuccess(false);
                return result;
            }
        }else {
            System.out.println("请输入用户名密码");
            result.setMsg("请输入用户名密码");
            result.setSuccess(false);
            return result;
        }
    }

    @RequestMapping("/getMemberById")
    @ResponseBody
    public  Result getMemberById(Integer id){
        Result<Object> result = new Result<>();
        if (id!=null){
            Member member = memberService.getMemberById(id);

            if(member!=null){
                result.setData(member);
                result.setMsg("成功");
                result.setSuccess(true);
                return result;
            }else {
                result.setMsg("没有此用户");
                result.setSuccess(false);
                return result;
            }
        }

        return result;
    }


    //修改用户列表
    @RequestMapping("/editMember")
    @ResponseBody
    public Result editMember(Member member){
        Result<Object> result = new Result<>();

        if (member!=null && member.getName()!=null
                && !member.getName().trim().equals("")
                && member.getId()!=null
        ){
            int i = memberService.editMember(member);
            System.out.println(i);
            if (i>0){
                result.setMsg("修改成功");
                result.setSuccess(true);
                return result;
            }else {
                result.setMsg("修改失败");
                result.setSuccess(false);
                return result;
            }
        }
        return result;
    }
    @RequestMapping("/editMemberPassword")
    @ResponseBody
    public Result editMemberPassword(Member member,HttpSession httpSession){
        Member memberSession = (Member) httpSession.getAttribute("MEMBER_SESSION");
        member.setId(memberSession.getId());
        Result<Object> result = new Result<>();

        if (member!=null && member.getEmail()!=null
                && !member.getEmail().trim().equals("")
        ){
            int i = memberService.editMember(member);
            System.out.println(i);
            if (i>0){
                result.setMsg("修改成功");
                result.setSuccess(true);
                return result;
            }else {
                result.setMsg("修改失败");
                result.setSuccess(false);
                return result;
            }
        }
        return result;
    }

    //删除用户列表
    @RequestMapping("/delMember")
    @ResponseBody
    public Result delMember(Integer id){
        Result<Object> result = new Result<>();
        int i = memberService.delById(id);
        if(i>0){
            result.setMsg("删除成功");
            result.setSuccess(true);
            return result;
        }else {
            result.setMsg("新增失败");
            result.setSuccess(false);
            return result;
        }
    }


    //注销
    @RequestMapping("/exitMember")
    public String exitMember(HttpSession httpSession){
        httpSession.invalidate();
        return "login";
    }


    //批量删除用户列表
    @RequestMapping("/delAllMember")
    @ResponseBody
    public Result delAllMember(String ids){
        Result<Object> result = new Result<>();
        System.out.println("收到删除数据");
        System.out.println(ids);
        String[] strs =ids.split(",");
        List<Integer> id = new ArrayList<>();
        System.out.println(strs.length);
        for(int i = 0;i<strs.length; i++){
            id.add(Integer.parseInt(strs[i].trim()));
//           ids.add(Integer.valueOf(strs[i]));
//            id.add(strs[i].length());
        }

        System.out.println(id.size());
        int i = memberService.delAll(id);
        if(i>0){
            result.setMsg("删除成功");
            result.setSuccess(true);
            return result;
        }else {
            result.setMsg("删除失败");
            result.setSuccess(false);
            return result;
        }

    }












}
